package com.cs.bd.mopub;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.cs.bd.R;
import com.cs.bd.ad.params.AdSdkParamsBuilder;
import com.cs.bd.ad.self.ClickHelper;
import com.mopub.nativeads.ViewBinder;

public class MopobViewGroupWarp extends FrameLayout implements View.OnClickListener {

    private ViewGroup mAdView;

    private ViewBinder viewHolder;

    public MopobViewGroupWarp(@NonNull Context context) {
        super(context);
    }

    public void renderAdView(ViewBinder viewHolder, boolean isExternalAds) {
        this.viewHolder = viewHolder;
        try {
            findViewById(viewHolder.privacyInformationIconImageId).setVisibility(View.GONE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        View btnView = null;
        try {
            btnView = findViewById(viewHolder.callToActionId);
            btnView.setOnClickListener(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        setText(btnView, "查看");
        try {
            if (isExternalAds) {
                String keyArray[] = getContext().getResources().getStringArray(R.array.show_key);
                int index = (int) (Math.random() * keyArray.length);
                setText(findViewById(viewHolder.titleId), keyArray[index]);
                setText(findViewById(viewHolder.textId), getContext().getResources().getStringArray(R.array.show_value)[index]);
            } else {
                String keyArray[] = getContext().getResources().getStringArray(R.array.inner_key);
                int index = (int) (Math.random() * keyArray.length);
                String valueArray[] = null;
                switch (index) {
                    case 0:
                        valueArray = getContext().getResources().getStringArray(R.array.key1);
                        break;
                    case 1:
                        valueArray = getContext().getResources().getStringArray(R.array.key2);
                        break;
                    case 2:
                        valueArray = getContext().getResources().getStringArray(R.array.key3);
                        break;
                    case 3:
                        valueArray = getContext().getResources().getStringArray(R.array.key4);
                        break;
                    case 4:
                        valueArray = getContext().getResources().getStringArray(R.array.key5);
                        break;
                }
                setText(findViewById(viewHolder.titleId), keyArray[index]);
                setText(findViewById(viewHolder.textId), valueArray[index]);
            }
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        requestLayout();
    }

    public void addAdView(ViewGroup mAdView) {
        this.mAdView = mAdView;
        addView(mAdView);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (viewHolder != null) {
            try {
                View bannerView = findViewById(viewHolder.mainImageId);
                mAdView.layout(bannerView.getLeft(), bannerView.getTop(), bannerView.getRight(), bannerView.getBottom());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View v) {
        ClickHelper.performClick(mAdView);
    }

    private void setText(View view, String text) {
        try {
            if (TextView.class.isInstance(view)) {
                TextView textView = (TextView) view;
                textView.setText(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
